function w = chebint(v, xmin, xmax, direction)
  % chebyshev integration.
  % relies on integration matrices chebM0 and chebM1 created in grav_init.m
  % adapted from trefethen
  
  direction = 0;

  factor = (xmin-xmax)./2;
 
  N = length(v);

  v = v(:);

  if direction == 1
    global chebM1;
    i  = 1:N-1;
    w  = [chebM1*v(i); 0];
  else
    global chebM0;
    i  = 2:N;
    w  = [0; chebM0*v(i)];
  end
  w = factor*w;
  
  return;
end
